import java.io.File;
import java.io.IOException;


public class ExOnFirstTimeCheck {
	 public static final String FIRST_TIME_CHECK_SAN_FILENAME = "TEMPSAN";
	 public static final String FIRST_TIME_CHECK_PEP_FILENAME = "TEMPEP";
	 public static final String FIRST_TIME_CHECK_RULE_FILENAME = "TEMPRULE";
	 public static final String FIRST_TIME_CHECK_RES_FILENAME = "TEMPRES";
	
	 public File logPath = new File("C:\\app\\cbf\\GLRD\\OPERATIONS\\logs");
	 public boolean isDayFirstTimeExecutionCheck(String firstTimeCheckFileName){
		String fileNameCreateCheck = null;
		try {
			if (firstTimeCheckFileName.trim().equals(FIRST_TIME_CHECK_SAN_FILENAME)) {
				fileNameCreateCheck = logPath.getPath()+File.separator+firstTimeCheckFileName+".txt";
				File FirstTimeCreateFileName = new File(fileNameCreateCheck);
				if(FirstTimeCreateFileName.exists()){
					//delete older files 
					deleteOlderFiles(logPath);
					
				}else{
					FirstTimeCreateFileName.createNewFile();
					return true;
				}
			}

			if (firstTimeCheckFileName.trim().equals(FIRST_TIME_CHECK_PEP_FILENAME)) {
				fileNameCreateCheck = logPath.getPath()+File.separator+firstTimeCheckFileName+".txt";
				File FirstTimeCreateFileName = new File(fileNameCreateCheck);
				
				if (FirstTimeCreateFileName.exists()) {
					//delete older files 
					deleteOlderFiles(logPath);
				} else {
					FirstTimeCreateFileName.createNewFile();
					return true;
				}
			}

			if (firstTimeCheckFileName.trim().equals(FIRST_TIME_CHECK_RULE_FILENAME)) {
				fileNameCreateCheck = logPath.getPath()+File.separator+firstTimeCheckFileName+".txt";
				File FirstTimeCreateFileName = new File(fileNameCreateCheck);
				
				if (FirstTimeCreateFileName.exists()) {
					//delete older files 
					deleteOlderFiles(logPath);
				} else {
					FirstTimeCreateFileName.createNewFile();
					return true;
				}
			}

			if (firstTimeCheckFileName.trim().equals(FIRST_TIME_CHECK_RES_FILENAME)) {
				fileNameCreateCheck = logPath.getPath()+File.separator+firstTimeCheckFileName+".txt";
				File FirstTimeCreateFileName = new File(fileNameCreateCheck);
				if (FirstTimeCreateFileName.exists()) {
					//delete older files 
					deleteOlderFiles(logPath);
				} else {
					FirstTimeCreateFileName.createNewFile();
					return true;
				}
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
			return false;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	private void deleteOlderFiles(File logPath) {
		File[] listFiles = logPath.listFiles();
		long purgeTime = System.currentTimeMillis()	- (30 * 24 * 60 * 60 * 1000);
		for (File listFile : listFiles) {
			if (listFile.lastModified() < purgeTime) {
				if (!listFile.delete()) {
					System.out.println("Unable to delete file: " + listFile);
				}
			}
		}

	}
	
	public static void main(String[] args) {
		
		String firstTimeCheckFileName ="TEMPSAN";
		ExOnFirstTimeCheck ex = new ExOnFirstTimeCheck();
		ex.isDayFirstTimeExecutionCheck(firstTimeCheckFileName);
		
		
	}

}
